package com.hnkywl.sts.service.organ;

import com.hnkywl.sts.dao.organ.LjLsDao;
import com.hnkywl.sts.dao.organ.LjwhDao;
import com.hnkywl.sts.dao.organ.QyjgDao;
import com.hnkywl.sts.entity.organ.*;
import com.hnkywl.sts.service.sys.YgService;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-3-21
 * Time: 下午5:13
 * To change this template use File | Settings | File Templates.
 */
@Service
public class LjLsService {
    private LjwhService ljwhService;
    private LjLsDao ljLsDao;
    private QyjgDao qyjgDao;
    private LjwhDao ljwhDao;

    /**
     * 查询供应商明细
     *
     * @param id 名称
     * @return
     */
    @Transactional
    public LjLs findLjLsDetail(Long id) {
        return this.ljLsDao.get(id);
    }

    /**
     * 保存供应商
     *
     * @param ljLs
     */
    @Transactional
    public void save(String syList, String xyList, LjLs ljLs) throws RuntimeException {
        List<LjGys> ljGyses = new ArrayList<LjGys>();
        String ids = new String();
        String lx = new String();
        if (!StringUtils.isEmpty(syList)) {
            ids = syList;
            lx = "sy";
        } else if (!StringUtils.isEmpty(xyList)) {
            ids = xyList;
            lx = "xy";
        }
        String[] idList = ids.split(",");
        validateLjLs(syList, xyList, ljLs);
        for (int i = 0; i < idList.length; i++) {
            LjGys ljGys = new LjGys();
            ljGys.setGys(qyjgDao.get(Long.parseLong(idList[i].trim())));
            ljGys.setLx(lx);
            ljGys.setLjls(ljLs);
            ljGys.setCreateUser(YgService.getCurrentYg());
            ljGys.setCreateOrgan(YgService.getCurrentYg().getZggs());
            ljGys.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            ljGys.setDateCreated(new Date());
            ljGys.setLastUpdated(ljGys.getDateCreated());
            ljGyses.add(ljGys);
        }
        ljLs.setLjGysList(ljGyses);
        ljLs.setZt("ybc");
        ljLs.setCreateUser(YgService.getCurrentYg());
        ljLs.setCreateOrgan(YgService.getCurrentYg().getZggs());
        ljLs.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        ljLs.setDateCreated(new Date());
        ljLs.setLastUpdated(ljLs.getDateCreated());
        ljLsDao.save(ljLs);
    }

    //验证修改供应商的重复验证
    private void validateLjLs(String syList, String xyList, LjLs ljLs) throws RuntimeException {
        List<Gsjg> syes = ljwhDao.get(ljLs.getLj().getId()).getSy();
        List<Gsjg> xyes = ljwhDao.get(ljLs.getLj().getId()).getXy();
        if (syes != null && syes.size() != 0 && !StringUtils.isEmpty(syList)) {
            String[] syIds = syList.split(",");
            for (int i = 0; i < syes.size(); i++) {
                for (int j = 0; j < syIds.length; j++) {
                    if (syes.get(i).getId() == Long.parseLong(syIds[j].trim())) {
                        throw new RuntimeException("路径中上游供应商已经存在(" + syes.get(i).getJc() + ")请重新选择!");
                    }
                }
            }
        }
        if (xyes != null && xyes.size() != 0 && !StringUtils.isEmpty(xyList)) {
            String[] xyIds = xyList.split(",");
            for (int i = 0; i < xyes.size(); i++) {
                for (int j = 0; j < xyIds.length; j++) {
                    if (xyes.get(i).getId() == Long.parseLong(xyIds[j].trim())) {
                        throw new RuntimeException("路径中下游客户已经存在(" + xyes.get(i).getJc() + ")请重新选择!");
                    }
                }
            }
        }
    }

    @Transactional
    public void updateLjLs(LjLs ljLs) {
        ljLs.setLastUpdated(new Date());
        this.ljLsDao.save(ljLs);
    }

    @Transactional
    public void transLj(LjLs ljLs) {
        Lj lj = ljLs.getLj();
        List<LjGys> ljGysList = ljLs.getLjGysList();
        for(LjGys ljGys:ljGysList){
            if(ljGys.getLx().equals("sy")){
                lj.getSy().add(ljGys.getGys());
            }else{
                lj.getXy().add(ljGys.getGys());
            }
        }
        this.ljwhService.update(lj);
    }

    /**
     * 查询临时路径明细
     *
     * @param processInstanceId 流程实体ID
     * @return
     */
    @Transactional
    public LjLs finLjLsDetailByProcessInstance(String processInstanceId) {
        return this.ljLsDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }

    @Autowired
    public void setLjLsDao(LjLsDao ljLsDao) {
        this.ljLsDao = ljLsDao;
    }

    @Autowired
    public void setQyjgDao(QyjgDao qyjgDao) {
        this.qyjgDao = qyjgDao;
    }

    @Autowired
    public void setLjwhDao(LjwhDao ljwhDao) {
        this.ljwhDao = ljwhDao;
    }

    @Autowired
    public void setLjwhService(LjwhService ljwhService) {
        this.ljwhService = ljwhService;
    }
}
